package cn.enjoy.tool.base;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;

public class SpEL {
    public static void main(String[] args) {
//        el0();
//        el();
        el2();
    }

    public static void el0() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("'hello'.concat('!')");
        String message = (String) exp.getValue();
        System.out.println(message);
        exp = parser.parseExpression("'hello'.bytes");
        byte[] bytes = (byte[]) exp.getValue();
        System.out.println(bytes);
    }

    public static void el() {
        GregorianCalendar c = new GregorianCalendar();
        c.set(1856, 7, 9);
//        Tenant tesla = new Tenant();
//        tesla.setAddress("dd");

        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("address");
//        String name = (String) exp.getValue(tesla);

        exp = parser.parseExpression("'hello'.concat('!')");
        String message = (String) exp.getValue();

        exp = parser.parseExpression("name == 'Nikola Tesla'");
//        boolean result = exp.getValue(tesla, Boolean.class);
    }

    public static void el2() {
        List primes = new ArrayList();
        primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable("primes",primes);
        List primesGreaterThanTen = (List) parser.parseExpression("#primes.?[#this>10]").getValue(context);
        primesGreaterThanTen.forEach(System.out::println);
    }
}
